<!DOCTYPE html>
<html>
<head>
    <title>SRS</title>
    <meta charset="utf-8">
    <style>
        body{
            padding-top: 55px;
        }
    </style>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
    <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="js/adapter-7.4.0.min.js"></script>
    <script type="text/javascript" src="js/winlin.utility.js"></script>
    <script type="text/javascript" src="js/srs.page.js"></script>
</head>
<body>
<img src='https://ossrs.net/gif/v1/sls.gif?site=ossrs.net&path=/player/rtcpublisher'/>
<div class="navbar navbar-fixed-top">
    <div class="navbar-inner">
        <div class="container">
            <a id="srs_index" class="brand" href="https://github.com/ossrs/srs">SRS</a>
            <div class="nav-collapse collapse">
                <ul class="nav">
                    <li><a id="nav_srs_player" href="srs_player.html">SRS播放器</a></li>
                    <li><a id="nav_rtc_player" href="rtc_player.html">RTC播放器</a></li>
                    <li class="active"><a id="nav_rtc_publisher" href="rtc_publisher.html">RTC推流</a></li>
                    <!--<li><a id="nav_srs_publisher" href="srs_publisher.html">SRS编码器</a></li>-->
                    <!--<li><a id="nav_srs_chat" href="srs_chat.html">SRS会议</a></li>-->
                    <!--<li><a id="nav_srs_bwt" href="srs_bwt.html">SRS测网速</a></li>-->
                    <!--<li><a id="nav_jwplayer6" href="jwplayer6.html">JWPlayer6播放器</a></li>-->
                    <!--<li><a id="nav_osmf" href="osmf.html">AdobeOSMF播放器</a></li>-->
                    <!--<li><a id="nav_vlc" href="vlc.html">VLC播放器</a></li>-->
                    <li><a id="nav_gb28181" href="srs_gb28181.html">GB28181</a></li>
                    <li><a href="https://github.com/ossrs/srs">源码</a></li>
                </ul>
            </div>
        </div>
    </div>
</div>
<div class="container">
    <div class="form-inline">
        URL:
        <input type="text" id="txt_url" class="input-xxlarge" value="">
        <button class="btn btn-primary" id="btn_publish">开始推流</button>
    </div>

    <label></label>
    <video id="rtc_media_player" width="320" autoplay muted></video>

    <label></label>
    SessionID: <span id='sessionid'></span>

    <label></label>
    Simulator: <a href='#' id='simulator-drop'>Drop</a>

    <footer>
        <p></p>
        <p><a href="https://github.com/ossrs/srs">SRS Team &copy; 2020</a></p>
    </footer>
</div>
<script type="text/javascript">
    var pc = null; // Global handler to do cleanup when replaying.
    $(function(){
        var startPublish = function() {
            $('#rtc_media_player').show();
            var urlObject = parse_rtmp_url($("#txt_url").val());
            var schema = window.location.protocol;

            // Close PC when user replay.
            if (pc) {
                pc.close();
            }

            pc = new RTCPeerConnection(null);
            pc.addTransceiver("audio", {direction: "sendonly"});
            pc.addTransceiver("video", {direction: "sendonly"});

            var constraints = {
                audio: true, video: {
                    height: { max: 320 }
                }
            };
            navigator.mediaDevices.getUserMedia(
                constraints
            ).then(function(stream) {
                console.log('Got stream with constraints: ', constraints);
                $('#rtc_media_player').prop('srcObject', stream);

                pc.addStream(stream);

                return new Promise(function(resolve, reject) {
                    pc.createOffer(function(offer){
                        resolve(offer);
                    },function(reason){
                        reject(reason);
                    });
                });
            }).then(function(offer) {
                return pc.setLocalDescription(offer).then(function(){ return offer; });
            }).then(function(offer) {
                return new Promise(function(resolve, reject) {
                    var port = urlObject.port || 1985;

                    // @see https://github.com/rtcdn/rtcdn-draft
                    var api = urlObject.user_query.publish || '/rtc/v1/publish/';
                    if (api.lastIndexOf('/') != api.length - 1) {
                        api += '/';
                    }

                    var url = schema + '//' + urlObject.server + ':' + port + api;
                    for (var key in urlObject.user_query) {
                        if (key != 'api' && key != 'publish') {
                            url += '&' + key + '=' + urlObject.user_query[key];
                        }
                    }
                    // Replace /rtc/v1/publish/&k=v to /rtc/v1/publish/?k=v
                    url = url.replace(api + '&', api + '?');

                    // @see https://github.com/rtcdn/rtcdn-draft
                    var data = {
                        api: url, streamurl: urlObject.url, clientip: null, sdp: offer.sdp
                    };
                    console.log("Generated offer: ", data);

                    $.ajax({
                        type: "POST", url: url, data: JSON.stringify(data),
                        contentType:'application/json', dataType: 'json'
                    }).done(function(data) {
                        console.log("Got answer: ", data);
                        if (data.code) {
                            reject(data); return;
                        }

                        var simulator = schema + '//' + urlObject.server + ':' + port + '/rtc/v1/nack/';
                        $('#sessionid').html(data.sessionid);
                        $('#simulator-drop').attr('href', simulator + '?drop=1&username=' + data.sessionid);
                        resolve(data.sdp);
                    }).fail(function(reason){
                        reject(reason);
                    });
                });
            }).then(function(answer) {
                return pc.setRemoteDescription(new RTCSessionDescription({type: 'answer', sdp: answer}));
            }).catch(function(reason) {
                pc.getLocalStreams().forEach(function(stream){
                    stream.getTracks().forEach(function(track) {
                        track.stop();
                    });
                });
                pc.close(); $('#rtc_media_player').hide();
                throw reason;
            });
        };

        $('#rtc_media_player').hide();
        var query = parse_query_string();
        srs_init_rtc("#txt_url", query);

        $("#btn_publish").click(startPublish);
        if (query.autostart === 'true') {
            startPublish();
        }
    });
</script>
</body>
</html>

</html>
